{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import  random\n",
    "import glob\n",
    "import os\n",
    "import sys\n",
    "try:\n",
    "    path = glob.glob('/opt/carla-simulator/PythonAPI/carla/dist/carla-0.9.13-py3.7-linux-x86_64.egg')[0]\n",
    "    sys.path.append(path)\n",
    "except IndexError:\n",
    "    pass\n",
    "import carla\n",
    "print(carla.__file__)\n",
    "\n",
    "client = carla.Client('localhost', 2000)\n",
    "print(client.get_available_maps())\n",
    "\n",
    "world = client.get_world()\n",
    "# world = client.load_world('Town06')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Retrieve the spectator object\n",
    "spectator = world.get_spectator()\n",
    "\n",
    "# Get the location and rotation of the spectator through its transform\n",
    "transform = spectator.get_transform()\n",
    "\n",
    "location = transform.location\n",
    "rotation = transform.rotation\n",
    "\n",
    "# Set the spectator with an empty transform\n",
    "spectator.set_transform(carla.Transform())\n",
    "# This will set the spectator at the origin of the map, with 0 degrees\n",
    "# pitch, yaw and roll - a good way to orient yourself in the map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get the blueprint library and filter for the vehicle blueprints\n",
    "vehicle_blueprints = world.get_blueprint_library().filter('*vehicle*')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get the map's spawn points\n",
    "spawn_points = world.get_map().get_spawn_points()\n",
    "# print(spawn_points)\n",
    "# Spawn 50 vehicles randomly distributed throughout the map \n",
    "# for each spawn point, we choose a random vehicle from the blueprint library\n",
    "for i in range(0,20):\n",
    "    world.try_spawn_actor(random.choice(vehicle_blueprints), random.choice(spawn_points))\n",
    "    \n",
    "ego_vehicle = world.spawn_actor(random.choice(vehicle_blueprints), random.choice(spawn_points))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a transform to place the camera on top of the vehicle\n",
    "camera_init_trans = carla.Transform(carla.Location(z=1.5))\n",
    "\n",
    "# We create the camera through a blueprint that defines its properties\n",
    "camera_bp = world.get_blueprint_library().find('sensor.camera.rgb')\n",
    "\n",
    "# We spawn the camera and attach it to our ego vehicle\n",
    "camera = world.spawn_actor(camera_bp, camera_init_trans, attach_to=ego_vehicle)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a transform to place the camera on top of the vehicle\n",
    "camera_init_trans = carla.Transform(carla.Location(z=1.5))\n",
    "\n",
    "# We create the camera through a blueprint that defines its properties\n",
    "camera_bp = world.get_blueprint_library().find('sensor.camera.rgb')\n",
    "\n",
    "# We spawn the camera and attach it to our ego vehicle\n",
    "camera = world.spawn_actor(camera_bp, camera_init_trans, attach_to=ego_vehicle)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Start camera with PyGame callback\n",
    "camera.listen(lambda image: image.save_to_disk('out/%06d.png' % image.frame))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(11)\n",
    "for vehicle in world.get_actors().filter('*vehicle*'):\n",
    "    print(i)\n",
    "    vehicle.set_autopilot(True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'sumolib'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[1;32m/home/zww/carla_learn/introduction.ipynb Cell 9\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> <a href='vscode-notebook-cell:/home/zww/carla_learn/introduction.ipynb#ch0000008?line=0'>1</a>\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39msumolib\u001b[39;00m  \u001b[39m# pylint: disable=import-error\u001b[39;00m\n\u001b[1;32m      <a href='vscode-notebook-cell:/home/zww/carla_learn/introduction.ipynb#ch0000008?line=1'>2</a>\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mtraci\u001b[39;00m\n",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'sumolib'"
     ]
    }
   ],
   "source": [
    "import sumolib  # pylint: disable=import-error\n",
    "import traci  # pylint: disable=import-error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.10 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
